package algorithm7.leetcode.剑指Offer;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class JZ58 {

    public String reverseLeftWords(String s, int n) {
//        return handle1(s, n);
        return handle2(s, n);
    }

    private String handle2(String s, int n) {
        return s.substring(n) + s.substring(0, n);
    }

    private String handle1(String s, int n) {
        char[] chars = s.toCharArray();
        int len = chars.length;
        StringBuilder sb = new StringBuilder();
        for (int i = n; i < len; i++) {
            sb.append(chars[i]);
        }
        for (int i = 0; i < n; i++) {
            sb.append(chars[i]);
        }
        return sb.toString();
    }

/*

剑指 Offer 58 - II. 左旋转字符串
字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如，输入字符串"abcdefg"和数字2，该函数将返回左旋转两位得到的结果"cdefgab"。

示例 1：
输入: s = "abcdefg", k = 2
输出: "cdefgab"

示例 2：
输入: s = "lrloseumgh", k = 6
输出: "umghlrlose"

 */

    @Test
    void t() {
        String s = "abcdefg";
        int n = 2;
        String res = reverseLeftWords(s, n);
        System.out.println("res = " + res);

        String s2 = "lrloseumgh";
        int n2 = 6;
        String res2 = reverseLeftWords(s2, n2);
        System.out.println("res2 = " + res2);

        Assertions.assertEquals("cdefgab", res);
        Assertions.assertEquals("umghlrlose", res2);
    }

}
